package com.ly.lw.styledo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ly.lw.styledo.R;
import com.ly.lw.styledo.bean.RecBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 分类/推荐/gridView适配器
 *
 * @author 王学玲
 *         Created by T400 on 15-7-6.
 */
public class CategoryGridAdapter extends AppBaseAdapter<RecBean> {

    private DisplayImageOptions options;

    public CategoryGridAdapter(List<RecBean> list, Context context) {
        super(list, context);
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageOnLoading(R.mipmap.ic_launcher);
        builder.showImageOnFail(R.mipmap.ic_launcher);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                .build();
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder vHolder=null;
        RecBean bean=list.get(position);
        if(convertView==null){
            convertView=inflater.inflate(R.layout.category_gridview_data,parent,false);
            vHolder=new ViewHolder(convertView);
            convertView.setTag(vHolder);
        }else {
            vHolder= (ViewHolder) convertView.getTag();
        }
        String imageUrl=bean.getCate_btn_img();
        ImageLoader.getInstance().displayImage(imageUrl, vHolder.image, options);
        if("女装".equals(bean.getDisp_cate1_nm_de())){
            vHolder.cate1.setBackgroundResource(R.color.lightpink);
        }else if("男装".equals(bean.getDisp_cate1_nm_de())){
            vHolder.cate1.setBackgroundResource(R.color.lightskyblue);
        }else if("鞋子".equals(bean.getDisp_cate1_nm_de())){
            vHolder.cate1.setBackgroundResource(R.color.chartreuse);
        }else {
            vHolder.cate1.setBackgroundResource(R.color.gold);
        }
        vHolder.cate1.setText(bean.getDisp_cate1_nm_de());//女装
        vHolder.name.setText(bean.getCate_nm());//T恤
        return convertView;
    }

    public static class ViewHolder{
        private ImageView image;
        private TextView cate1,name;

        public ViewHolder(View itemView) {
            image= (ImageView) itemView.findViewById(R.id.category_grid_image);
            cate1= (TextView) itemView.findViewById(R.id.category_grid_cate1);
            name= (TextView) itemView.findViewById(R.id.category_grid_name);
        }
    }

}
